package com.util;

import java.util.Calendar;

public class DateUtil {


    public static String getCurrentDate() {
        Calendar calendar = Calendar.getInstance();

        return addZero(new StringBuffer().append(calendar.get(Calendar.YEAR)).append("-").append(calendar.get(Calendar.MONTH) + 1).append("-").append(calendar.get(Calendar.DAY_OF_MONTH)).toString());
    }


    public static int getDifferDay(String first, String second) {
        int compareResult = compareDate(first, second);
        int differDays = 0;
        switch (compareResult) {
            case 0:
                differDays = 0;
                break;
            case 1: {
                String formFirst = addZero(first);
                String formSecond = addZero(second);
                differDays = 1;
                while (!getOffsetDate(differDays, formSecond)
                        .equals(formFirst)) {
                    differDays++;
                }
            }
            break;
            case 2: {
                String formFirst = addZero(first);
                String formSecond = addZero(second);
                differDays = 1;
                while (!getOffsetDate(differDays, formFirst).equals(
                        formSecond)) {
                    differDays++;
                }
            }
            break;
        }
        return differDays;
    }


    public static int compareDate(String first, String second) {
        String items1[] = splitDate(first);
        int firstValue[] = new int[]{Integer.valueOf(items1[0]),
                Integer.valueOf(items1[1]), Integer.valueOf(items1[2])};
        String items2[] = splitDate(second);
        int secondValue[] = new int[]{Integer.valueOf(items2[0]),
                Integer.valueOf(items2[1]), Integer.valueOf(items2[2])};
        int compareResult;
        if (firstValue[0] == secondValue[0]) {
            if (firstValue[1] == secondValue[1]) {
                if (firstValue[2] == secondValue[2]) {
                    compareResult = 0;
                } else if (firstValue[2] > secondValue[2]) {
                    compareResult = 1;
                } else {
                    compareResult = 2;
                }
            } else if (firstValue[1] > secondValue[1]) {
                compareResult = 1;
            } else {
                compareResult = 2;
            }
        } else if (firstValue[0] > secondValue[0]) {
            compareResult = 1;
        } else {
            compareResult = 2;
        }
        return compareResult;
    }


    public static String addZero(String date) {
        String[] items = date.split("-");
        if (items[1].length() < 2) {
            items[1] = "0" + items[1];
        }
        if (items[2].length() < 2) {
            items[2] = "0" + items[2];
        }
        return new StringBuffer().append(items[0]).append("-").append(items[1])
                .append("-").append(items[2]).toString();
    }


    public static String[] splitDate(String date) {
        String[] dates = date.split("-");
        if (dates[1].length() == 2 && dates[1].startsWith("0")) {
            dates[1] = dates[1].replace("0", "");
        }
        if (dates[2].length() == 2 && dates[2].startsWith("0")) {
            dates[2] = dates[2].replace("0", "");
        }
        return dates;
    }


    public static String getOffsetDate(int dayNumber, String date) {
        String dates[] = splitDate(date);
        int datesValue[] = new int[]{Integer.valueOf(dates[0]),
                Integer.valueOf(dates[1]), Integer.valueOf(dates[2])};
        if (dayNumber > 0) {
            for (int i = 0; i < dayNumber; i++) {
                datesValue[2]++;
                switch (datesValue[1]) {
                    case 2:
                        if (datesValue[0] % 4 == 0) {
                            if (datesValue[2] > 29) {
                                datesValue[1]++;
                                datesValue[2] = 1;
                            }
                        } else {
                            if (datesValue[2] > 28) {
                                datesValue[1]++;
                                datesValue[2] = 1;
                            }
                        }
                        break;
                    case 12:
                        if (datesValue[2] > 31) {
                            datesValue[0]++;
                            datesValue[1] = 1;
                            datesValue[2] = 1;
                        }
                        break;
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                        if (datesValue[2] > 31) {
                            datesValue[1]++;
                            datesValue[2] = 1;
                        }
                        break;
                    default:
                        if (datesValue[2] > 30) {
                            datesValue[1]++;
                            datesValue[2] = 1;
                        }
                        break;
                }
            }
        } else {
            for (int i = 0; i < Math.abs(dayNumber); i++) {
                datesValue[2]--;
                switch (datesValue[1]) {
                    case 3:
                        if (datesValue[0] % 4 == 0) {
                            if (datesValue[2] < 1) {
                                datesValue[1]--;
                                datesValue[2] = 29;
                            }
                        } else {
                            if (datesValue[2] < 1) {
                                datesValue[1]--;
                                datesValue[2] = 28;
                            }
                        }
                        break;
                    case 1:
                        if (datesValue[2] < 1) {
                            datesValue[0]--;
                            datesValue[1] = 12;
                            datesValue[2] = 31;
                        }
                        break;
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        if (datesValue[2] < 1) {
                            datesValue[1]--;
                            datesValue[2] = 30;
                        }
                        break;
                    default:
                        if (datesValue[2] < 1) {
                            datesValue[1]--;
                            datesValue[2] = 31;
                        }
                        break;
                }
            }
        }
        return addZero(new StringBuffer().append(datesValue[0]).append("-").append(datesValue[1]).append("-").append(datesValue[2]).toString());
    }
}
